package com.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

public class RegistServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取Session种的验证码
        String token = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        //删除Session种的验证码，使用一次后马上删除，可以防止用户回退导致的多次输入数据库
        req.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
        //获取用户输入的信息
        String code = req.getParameter("code");
        String username = req.getParameter("username");
        //判断验证码是否正确
        if (token!=null && token.equalsIgnoreCase(code)){
            System.out.println("保存到数据库："+username);
            resp.sendRedirect(req.getContextPath()+"/regist_success.jsp");
        }else{
            System.out.println("验证码有误！");
        }
    }
}
